﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;

namespace aucn.web.au.code
{
    public class NewsItem
    {
        protected string _title = string.Empty, _content = string.Empty, _id = string.Empty, _subTitle = string.Empty;
        protected DateTime _pdate = new DateTime();
        public string Title { get { return this._title; } set { this._title = value; } }
        public string SubTitle { get { return this._subTitle; } set { this._subTitle = value; } }
        public DateTime PublishDate { get { return this._pdate; } set { this._pdate = value; } }
        public string Content { get { return this._content; } set { this._content = value; } }
        public string ID { get { return this._id; } set { this._id = value; } }

        public NewsItem(string id, string t, string c, DateTime pdate)
        {
            this._title = t;
            this._content = c;
            this._id = id;
            this._pdate = pdate;
        }

        public NewsItem(string id, string title, string subTitle, string content, DateTime pdate)
        {
            this._title = title;
            this._subTitle = subTitle;
            this._content = content;
            this._id = id;
            this._pdate = pdate;
        }

        public static NewsItem FromXml(string id)
        {
            NewsItem news = null;
            XmlDocument doc = new XmlDocument();

            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/news.xml"));
            XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
            xm.AddNamespace("x", "http://tempuri.org/news.xsd");

            XmlNode node = doc.DocumentElement.SelectSingleNode("x:NewsItem[@ID='" + id + "']", xm);
            if (node != null)
            {
                XmlNode subTitleNode = node.SelectSingleNode("x:SubTitle", xm);
                if (subTitleNode == null)
                {
                    news = new NewsItem(id, node.SelectSingleNode("x:Title", xm).InnerText, node.SelectSingleNode("x:Content", xm).InnerText, Convert.ToDateTime(node.Attributes["PublishDate"].Value));
                }
                else
                {
                    news = new NewsItem(id, node.SelectSingleNode("x:Title", xm).InnerText, subTitleNode.InnerText, node.SelectSingleNode("x:Content", xm).InnerText, Convert.ToDateTime(node.Attributes["PublishDate"].Value));
                }
            }

            return news;
        }
    }

    public class NewsCollection
    {
        protected List<NewsItem> _list;
        public List<NewsItem> NewsList { get { return this._list; } }
        public NewsCollection()
        {
            this._list = new List<NewsItem>();
            XmlDocument doc = new XmlDocument();

            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/news.xml"));
            XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
            xm.AddNamespace("x", "http://tempuri.org/news.xsd");

            XmlNodeList nodeList = doc.DocumentElement.SelectNodes("x:NewsItem", xm);
            foreach (XmlNode node in nodeList)
            {
                XmlNode subTitleNode = node.SelectSingleNode("x:SubTitle", xm);
                NewsItem news;
                if (subTitleNode != null)
                {
                    news = new NewsItem(node.Attributes["ID"].Value, node.SelectSingleNode("x:Title", xm).InnerText, subTitleNode.InnerText, node.SelectSingleNode("x:Content", xm).InnerText, Convert.ToDateTime(node.Attributes["PublishDate"].Value));
                }
                else
                {
                    news = new NewsItem(node.Attributes["ID"].Value, node.SelectSingleNode("x:Title", xm).InnerText, node.SelectSingleNode("x:Content", xm).InnerText, Convert.ToDateTime(node.Attributes["PublishDate"].Value));
                }
                this._list.Add(news);
            }
        }
    }
}
